package ArrayTest;

import java.util.Arrays;

public class QuickSort2 {
    public static void main(String[] args) {
        int []arr={5,7,3,1,6,9,4,2,8,0};
        quickSort2(arr,0,arr.length-1);
        System.out.println(Arrays.toString(arr));
    }
    public static void quickSort2(int[] arr,int start,int end){
        int x=start;
        int y=end;
        int pivot=arr[end];
        while (x<y){
            while (x<y&&arr[y]>pivot){
                y--;
            }
            while (x<y &&arr[x]<pivot){
                x++;
            }
            if (x<y&&arr[x]==arr[y]){
                x++;
            }else {
                int t=arr[x];
                arr[x]=arr[y];
                arr[y]=t;
                System.out.println(Arrays.toString(arr));
            }
        }
        //最后一次会有x++和y++ 所以x和y比实际大1
        if (x-1>start){
            quickSort2(arr,start,x-1);
        }
        if (y+1<end){
            quickSort2(arr,y+1,end);
        }
    }
}
